/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dragonboy
 */
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.Data_variante;
import util.HTMLFilter;
import util.Lucene_Index;
public class GreetingServlet extends HttpServlet{
    protected String displayName;
    protected int userType;
    
    private List<String> process_index_log(File _indexPath){
        List<String> list_date = new ArrayList<String>();
        File [] listfile = _indexPath.listFiles();
        for (int i = 0; i < listfile.length; i++) {
            if(!list_date.contains(listfile[i].getName().toString()))
            { 
                list_date.add(listfile[i].getName().toString());
            }
        }
        return list_date; 
    }
    
    ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        //index();
        String name = "username";
        String url_changepw="";
        String url_logout="../servlet/logout";
        File indexDir = new File(Data_variante.dir_index);
        List<String> list_date = process_index_log(indexDir);
        Collections.sort(list_date);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        if(session == null){
            response.sendRedirect("../index.html");
            return;
        }
        userType = Integer.valueOf((String) session.getAttribute("userType"));
        //String currUserName = (String)session.getAttribute("username");
        String username = (String) session.getAttribute("displayName");
        //out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
        //out.println("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"");
        //out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html\"> ");
        //out.println("<link type=\"image/x-icon\" href=\"/resource/images/favicon.ico\" rel=\"shortcut icon\">");
        out.println("<title>"+ rb.getString("greeting.title") +"</title>");
        out.println("<script type=\"text/javascript\" src=\"../js/jquery.js\"></script>");
        out.println("<script type=\"text/javascript\" src=\"../js/quickpager.jquery.js\"></script>");
        //out.println("<script type=\"text/javascript\" src=\"../js/jquery.min.js\"></script>");
        out.println("<script type=\"text/javascript\" src=\"../js/jquery.paginate.js\"></script>");
        out.println("<script type=\"text/javascript\" src=\"../js/treemenu.js\"></script>");
        out.println("<script type=\"text/javascript\" src=\"../js/ajaxsbmt.js\"></script>");
        //<!-- stylesheet for page -->
        out.println("<link type=\"text/css\" href=\"../css/layout.css\" rel=\"stylesheet\" />");
        out.println("<link type=\"text/css\" href=\"../css/main.css\" rel=\"stylesheet\" />");
        out.println("<link type=\"text/css\" href=\"../css/style.css\" rel=\"stylesheet\" />");
        //insert script
        out.println("<script type=\"text/javascript\">");
            output_menu(out);
            function(out);
        out.println("</script>");
        out.println("</head>");
        
        out.println("<body>");
            out.println("<div id=\"header\">");
                out.println("<div style=\"text-align: left;\">");
                    out.print("<a href=\"/\" title=\"Content Filter System\">");
                        out.print("<img id=\"logo\" SRC=\"../images/zinglogo.gif\" HSPACE=4 BORDER=0 align=TOP alt=\"Content Filter System\">");
                    out.println("</a>");
                    out.println("<div id=\"main-header\">");
                    out.println("<br /><br /><br /><br />");
                    //Cookie [] _cookies = request.getCookies();
                    
                    if(username == null){
                        response.sendRedirect("../index.html");
                        return;
                    }
                        out.println("Hi, <b>"+ HTMLFilter.filter(username) + /*" | <a href=\"" + HTMLFilter.filter(url_changepw) +"\">Change password</a>" */ "</b> | <a href=\""+ HTMLFilter.filter(url_logout)+"\">Logout</a>");
                    out.println("</div>");
                out.println("</div>");
                out.println("<div class=\"underline\"></div>");
            out.println("</div>");
            out.println("<div id=\"content\">");
                out.println("<div id=\"main-menu\">");
                    insert_menu_script(out);
                out.println("</div>");
                out.println("<div id=\"main-content\">");
                //show form
                out.println("<div id=\"content-comment\">"
                    
                    + "<div id=\"content-header\">"
                    + "Content filter &gt; <b>Comment</b>"
                    + "</div>"
                    + "<div id=\"content-body\">"
                    + "<div id=\"content-form\">"
                    + "<div id=\"header-tip\" style=\"color: red; font-size: 13px; margin-left: 200px;\"></div>"
                    + "<form name=\"SearchContentComment\" action=\"Comment_show\" method=\"POST\" onsubmit=\"xmlhttpPost(\'Comment_show\', \'SearchContentComment\', \'content-action\', \'\'); return false;\">"
                    + "<table id=\"comment-table\" cellspacing=\"10\">"
                    + "<tbody>");
            //show choosingDate_form
            out.println("<tr class=\"row\">"
                    +"<td class=\"col1\">"+ Data_variante.SearchForm_Date+"</td>"
                    +"<td class=\"col2\">"
                    +"<select name=\"date\" class=\"form-content-select\">");
            //todo decision how many day in log data
            for(int c = 0;c < list_date.size(); c++){
                if( c == 0) out.println("<option value=\""+ list_date.get(c) + "\" selected=\"selected\">" + list_date.get(c) + "</option>");
                else out.println("<option value=\""+ list_date.get(c) + "\">" + list_date.get(c) + "</option>");
            }
            out.println("</select>"          
                        +"</td>"
                        +"</tr>");
            
            //show choosingApp_form
            out.println( "<tr class=\"row\">"
                    + "<td class=\"col1\">"+ Data_variante.SearchForm_App+"</td>"
                    + "<td class=\"col2\">"
                    + "<select name=\"app\" class=\"form-content-select\" id=\"select-app\">"
                    + "<option value=\"All\">All</option>"
                    + "<option value=\"16\" Disabled>123Mua (Product)</option>"
                    + "<option value=\"3\" Disabled>Album</option>"
                    + "<option value=\"1\" Disabled>Blog</option>"
                    + "<option value=\"4\" Disabled>Feed</option>"
                    + "<option value=\"20\" Disabled>Link</option>"
                    + "<option value=\"21\" Disabled>Live</option>"
                    + "<option value=\"19\" Disabled>Mp3-Playlist (Product)</option>"
                    + "<option value=\"17\" Disabled>Mp3-Song (Product)</option>"
                    + "<option value=\"18\" Disabled>Mp3-Video (Product)</option>"
                    + "<option value=\"2\" Disabled>Photo</option>"
                    + "<option value=\"5\" Disabled>Q&amp;A (Product)</option>"
                    + "</select>"
                    + "</td>"
                    + "</tr>");
            //show chosingtype_form
            out.println("<tr class=\"row\">"
                    + "<td class=\"col1\">"+ Data_variante.SearchForm_Type +"</td>"
                    + "<td class=\"col2\">"
                    + "<select name=\"Type\" class=\"form-content-select\" id=\"select-type\">"
                    + "<option value=\"3\">Bad</option>"
                    + "<option value=\"4\">Unsured Polictics</option>"
                    + "<option value=\"5\">Unsured Common</option>"
                    + "<option value=\"0\">Normal</option>"
                    + "</select>"
                    + "</td>"
                    + "</tr>");
            //show search button
            out.println("<tr class=\"row\">"
                    +"<td class=\"col1\"></td>"
                    +"<td class=\"col2\">"
                    +"<input type=\"submit\" value=\"Search\" name=\"Search\" class=\"form-content-submit\">"
                    +"</td>"
                    +"</tr>"
                    +"</tbody></table>"
                    +"</form>"
                    +"</div>"
                    +"<br class=\"clear-all\">"
                    +"<div id=\"content-action\">"
                    +"<div id=\"header-tip\"></div>"
                    +"<form name=\"HanderContentComment\" action=\"/submit_action\" method=\"POST\" onsubmit=\"xmlhttpPost(\'submit_action\', \'HanderContentComment\', \'content-action\', \'\'); return false;\">"
                    +"Total: 0                                <br class=\"clear-all\">"
                    +"<br class=\"clear-all\">"
                    +"<table id=\"content-action-table\" cellpadding=\"0\" cellspacing=\"0\">"
                    +"<thead>"
                    +"<tr>"
                    +"<th class=\"table-the-first\">"+ Data_variante.table_col1_name +"</th>"
                    +"<th class=\"table-1\">"+ Data_variante.table_col2_name +"</th>"
                    +"<th class=\"table-7\">"+ Data_variante.table_col8_name +"</th>"
                    +"<th class=\"table-2\">"+ Data_variante.table_col3_name +"</th>"
                    +"<th class=\"table-3\">"+ Data_variante.table_col4_name("3") +"&nbsp;</th>"
                    +"<th class=\"table-4\">"+ Data_variante.table_col5_name("3") +"&nbsp;</th>"
                    +"<th class=\"table-5\">"+ Data_variante.table_col6_name + "</th>"
                    +"<th class=\"table-6\">"+ Data_variante.table_col7_name +"</th>"
                    +"</tr>"
                    +"</thead>"
                    +"<tbody id=\"table-body\">"
                    +"</tbody>"
                    +"</table>"
                    +"<br class=\"clear-all\">"
                    +"<br class=\"clear-all\">"
                    +"</form>"
                    +"<div class=\"pagination\">"
                    +"</div>"
                    +"</div>"
                    +"<div id=\"content-footer\">"
                    +"</div>"
                    +"</div>"
                    +"</div></div>"
               );    
                    
                //Add loading
                out.println("<div id=\"loading\">");
                    out.println("<p><img src=\"../images/ajax-loader.gif\" /></p>");
                out.println("</div>");
                out.println("<br class=\"clear-all\">");
            out.println("</div>");
            out.println("<div class=\"underline\"></div>");
            out.println("<br class=\"clear-all\"/>");
            out.println("<div id=\"footer\">");
                out.println("Copyright © Zing<br />Đơn vị chủ quản: VNG Corporation");
            out.println("</div>");
        out.println("</body>");
    out.println("</html>");
}
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        doPost(request, response);
        /*response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
            out.println("<head>");
            out.println("</head>");
            out.println("<body>");
                out.println("hello");
            out.println("</body>");
        out.println("</html>");*/
    }
    private void insert_menu_script(PrintWriter out){
        out.println("<script type=\"text/javascript\">");
            out.println("output_menu(\"../images/\");");
        out.println("</script>");
    }
    private void output_menu(PrintWriter out){
        out.println("function output_menu(linkImage) {");
             out.println("var mytree = new treeview(\"tree1\",linkImage+\"boxclose.gif\",linkImage+\"boxopen.gif\",linkImage+\"foldclose.gif\",linkImage+\"foldopen.gif\",linkImage+\"folditem.gif\",linkImage+\"line_top_corner.gif\",linkImage+\"line_branch.gif\",linkImage+\"line_bottom_corner.gif\",linkImage+\"line_vertical.gif\",linkImage+\"blank.gif\",16,22,true,false,true,230,0,20,0,0,true,false,false,true,false,\"\",\"\",\"no-repeat\",\"None\",1,\"\",0,\"\",\"\",\"\",\"no-repeat\",\"\",\"\",\"no-repeat\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"left\");");
                 out.println("mytree.addTree(\"Content filter\",\"#\",\"\",\"\",\"0\",\"box0\",\"icon0\",false,true,true,false,\"\",\"\",true,\"\",\"\",\"no-repeat\",\"\",\"\",\"no-repeat\",true,\"\",\"\",\"\",\"\",\"\",\"\",\"\");");
                 out.println("mytree.addNode(\"0\",false,true,false);");
                 out.println("mytree.addItem(\"Comment\",\"#content_comment\",\"\",\"content_comment\",false,false,false,\"\",true,\"\",\"\",\"no-repeat\",\"\",\"\",\"no-repeat\",true,\"\",\"\",\"\",\"\",\"\",\"\",\"\");");
                 out.println("mytree.endNode(false,true);");
                 if(userType > 1){
                     out.println("mytree.addTree(\"Admin_Box\",\"#\",\"\",\"\",\"1\",\"box1\",\"icon1\",false,true,true,false,\"\",\"\",true,\"\",\"\",\"no-repeat\",\"\",\"\",\"no-repeat\",true,\"\",\"\",\"\",\"\",\"\",\"\",\"\");");
                     //out.println("mytree.addNode(\"1\",false,true,false);");
                     out.println("mytree.addNode(\"1\",false,true,false);");
                     //out.println("mytree.addItem(\"Add User\",\"#add_user\",\"\",\"add_user\",false,false,false,\"\",true,\"\",\"\",\"no-repeat\",\"\",\"\",\"no-repeat\",true,\"\",\"\",\"\",\"\",\"\",\"\",\"\");");
                     out.println("mytree.addItem(\"Add User\",\"#add_user\",\"\",\"add_user\",false,false,false,\"\",false,\"\",\"\",\"no-repeat\",\"\",\"\",\"no-repeat\",false,\"\",\"\",\"\",\"\",\"\",\"\",\"\");");
                     out.println("mytree.endNode(false,true);");
                 }
                 out.println("mytree.showTree();");
        out.println("}");
    }
    private void function(PrintWriter out){
        out.println("$(document).ready(function() {");
            out.println("$(\'#main-menu a\').click(function(){");
                out.println("var titleClicked = $(this).attr(\'title\');");
                out.println("if(titleClicked != \'\') {");
                    out.println("$(\"#loading\").show();");
                    out.println("$(\'.menu-selected\').removeClass(\'menu-selected\');");
                    out.println("$(this).addClass(\'menu-selected\');");
                    //out.println("var arrayText = titleClicked.split(\"_\");");
                    //out.println("var linkAjax = \'/\' + arrayText[\'0\'] + \'/\' + arrayText[\'1\'];");
                    out.println("var linkAjax = titleClicked");
                    out.println("$.ajax({");
                        out.println("url :  linkAjax,");
                        out.println("success: function(data) {");
                            out.println("if(data != \'Not Permission\') {");
                                out.println("$(\"#main-content\").html(data);");
                            out.println("} else {");
                                out.println("location.reload();");
                            out.println("}");
                        out.println("}");
                    out.println("});");
                    out.println("$(\"#loading\").hide();");
                out.println("}");
            out.println("});");
        out.println("});");
    }
}
